package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.*;
import com.vo.ParkingLotVo;
import com.vo.Result;
import com.vo.StopcarmoneyVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
@RequestMapping("/ParkingLotInfo")
public class CommunityParkingController {
    @Resource
    private ParkingLotService parkingLotService;
    @Resource
    private StartStopService startStopService;
    @Resource
    private StopcarmoneyService stopCarMoneyService;
    @Resource
    private OwnersService ownerService;
    @Resource
    private ParkruleService parkRuleService;
    @Resource
    private ParkService parkService;
    @Resource
    private TrafficrecordsService trafficreCordsService;

    @Resource
    private VehicleService vehicleService;
    @RequestMapping("/toParkingLotInfo")
    public String toParkingLotInfo(){
        return "/CommunityParking/ParkingInfo";
    }
    @ResponseBody
    @RequestMapping("/SelectAllLinShiParkingLot")
    public Result selectAllLinShiParkingLot(ParkingLotVo parkingLotVo){
        PageInfo<Parkinglot> parkinglots = parkingLotService.selectAllLinShi(parkingLotVo);
        return Result.success(parkinglots);
    }
    @ResponseBody
    @RequestMapping("/selectStartStopByOwnerId")
    public Result selectStartStopByOwnerId(Integer ownerid){
        List<Startstop> startstops = startStopService.selectStartStopByOwnerId(ownerid);
        return Result.success(startstops);
    }
    @ResponseBody
    @RequestMapping("/addStartStop")
    public Result addStartStop(Startstop startstop){

        try {
            int i = startStopService.addStartStop(startstop);
            return Result.success("开始计时");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("停车失败");
        }

    }
    @ResponseBody
    @RequestMapping("/updateParkingLotStateById")
    public Result updateParkingLotStateById(Integer id){
        try {
            int i = parkingLotService.updateStateById(id);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }

    }
    @ResponseBody
    @RequestMapping("/addStopCarMoney")
    public Result addStopCarMoney(@RequestBody StopcarmoneyVo stopcarmoneyVo) {
        System.out.println(stopcarmoneyVo);
        Owners owners = ownerService.selectOwnerById(stopcarmoneyVo.getOwnerid());
        stopcarmoneyVo.setOwnername(owners.getOwnername());
        stopcarmoneyVo.setOwnerphone(owners.getOwnerphone());
        Parkinglot parkinglot = parkingLotService.selectParkingLotById(stopcarmoneyVo.getParkinglotid());
        stopcarmoneyVo.setParklotname(parkinglot.getParkingcode());
        Park park = parkService.selectParkById(stopcarmoneyVo.getParkid());
        stopcarmoneyVo.setParkname(park.getParkcode());
        String time=stopcarmoneyVo.getStarttime();
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime starttime=LocalDateTime.parse(time,dateTimeFormatter);

        LocalDateTime localDateTime = LocalDateTime.now();
        String endtime = localDateTime.toString();
        stopcarmoneyVo.setEndtime(endtime);
        Vehicle vehicle = vehicleService.selectVehicleById(stopcarmoneyVo.getCarid());
        stopcarmoneyVo.setCarno(vehicle.getCarno());


        Parkrule parkrule = parkRuleService.selectParkRuleById(stopcarmoneyVo.getRuleid());
        stopcarmoneyVo.setMoney(parkrule.getMoney());
        System.out.println(stopcarmoneyVo.getStarttime());

        // 计算差异
        Duration duration = Duration.between(starttime,localDateTime);
        Integer freetime=parkrule.getFreetime();
        long minutes = duration.toMinutes(); // 相差的小时数
        long m=0;
        long mm=0;
        long zong=0;
        if(minutes<freetime){
            stopcarmoneyVo.setIsno(1);
            stopcarmoneyVo.setZomoney(0);

            int i1 = parkingLotService.updateNullStateById(stopcarmoneyVo.getParkinglotid());
            //删除开始停车表里的信息
            int i2 = startStopService.deleteStartStopById(stopcarmoneyVo.getId());
            int i = stopCarMoneyService.addStopCarMoney(stopcarmoneyVo);
            int i3 = trafficreCordsService.addTrafficreCords(stopcarmoneyVo);
            return Result.success("已离开");
        }else if(minutes>freetime){
            stopcarmoneyVo.setIsno(0);
            //一天总钱数/小时数=收费小时数
            int moneyhours=parkrule.getDaymoney()/parkrule.getMoney();
            //停车分钟数/60=计算总天数；分两种情况 1、总天数小于收费小时数 2、大于一天
            double totalhours=minutes/60;//总小时数
                long totalday=minutes/60/24;//总天数
                long other=minutes-totalday*24*60;//除去整天之外的剩余分钟数
                long otherhours=other/60;//剩余的小时
                double otherhoursmoney=otherhours*parkrule.getMoney();//剩余的钱数
                double totaldaymoney=totalday*parkrule.getDaymoney();//总天数的前
                double zonghoursdaymoney=otherhoursmoney+totaldaymoney;//总钱数
                stopcarmoneyVo.setZomoney((int) zonghoursdaymoney);
                //修改车位状态
                int i1 = parkingLotService.updateNullStateById(stopcarmoneyVo.getParkinglotid());
                //删除开始停车表里的信息
                int i2 = startStopService.deleteStartStopById(stopcarmoneyVo.getId());
                int i = stopCarMoneyService.addStopCarMoney(stopcarmoneyVo);
                //添加通行记录
                int i3 = trafficreCordsService.addTrafficreCords(stopcarmoneyVo);
                return Result.success("请前往缴费");
        }
        return Result.success("已离开");
    }

}
